﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Schema;
using System.Xml;
using System.IO;
using NUnit.Framework;

namespace LinqtoCRM.Tests
{
    public class ValidatingNonExecutingCrmService : ICrmService
    {
        
        private XmlSchema _fetchXmlSchema;

        public ValidatingNonExecutingCrmService(string schema)
        {
            using(var reader =  new XmlTextReader(schema)){
                _fetchXmlSchema = XmlSchema.Read(
                    reader, (sender, args) => { if(args.Exception != null) throw args.Exception; });
            }
        }

        public string Fetch(string fetchXml)
        {
            //string dummyResult = "<resultset morerecords='0'><result/></resultset>";
            string dummyResult = "<resultset morerecords=\"0\" />";
            
            XmlDocument doc = new XmlDocument();
            doc.Schemas.Add(_fetchXmlSchema);
            doc.LoadXml(fetchXml);
            bool valid = true;
            string message = "";
			//ValidationEventArgs validationArgs;
            doc.Validate(
                (sender, args) => 
                    { 
                        valid = args.Exception == null; 
                        message = args.Exception.Message; 
                    }
                );
            
            Assert.IsTrue(valid,message);

            return dummyResult;
        }

        public System.Net.ICredentials Credentials
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public string Url
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }        
    }
}
